package com.doudou.base.resources.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doudou.base.resources.dao.UserDetailsInfoDao;
import com.doudou.base.resources.entity.UserDetailsInfo;
import com.doudou.base.resources.entity.dto.user.details.UserDetailsInfoAddDto;
import com.doudou.base.resources.entity.dto.user.details.UserDetailsInfoUpdateDto;
import com.doudou.base.resources.service.UserDetailsInfoService;
import com.doudou.client.entity.UserDetailsInfoDto;
import com.doudou.commons.utils.ConvertBeanUtils;
import com.doudou.commons.utils.ListUtils;
import com.doudou.commons.utils.MyAssert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**>
 * 用户的详细信息 服务实现类
 *
 * @author 傻男人
 * @since 2020-05-14
 */
@Slf4j
@Service
public class UserDetailsInfoServiceImpl  implements UserDetailsInfoService {

    @Autowired
    UserDetailsInfoDao userDetailsInfoDao;

    /**
    * 添加用户的详细信息
    * @param userDetailsInfoAddDto
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserDetailsInfoDto addUserDetailsInfo(UserDetailsInfoAddDto userDetailsInfoAddDto) {
        UserDetailsInfo userDetailsInfo = ConvertBeanUtils.copyProperties(userDetailsInfoAddDto,UserDetailsInfo.class);
        userDetailsInfoDao.insert(userDetailsInfo);
        return ConvertBeanUtils.copyProperties(userDetailsInfo,UserDetailsInfoDto.class);
    }

    /**
     * 根据ids删除用户的详细信息
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteUserDetailsInfo(String ids) {
        List<Long> listId = ListUtils.strToLongList(ids);
        QueryWrapper<UserDetailsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(UserDetailsInfo.Meta.id,listId);

        UserDetailsInfo userDetailsInfo = new UserDetailsInfo();
        userDetailsInfo .setDelFlag(1);
        userDetailsInfo .setUpdateDate(new Date());
        return userDetailsInfoDao.update(userDetailsInfo, queryWrapper);
    }

    /**
     * 根据id修改用户的详细信息
     * @param userDetailsInfoUpdateDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateUserDetailsInfo(UserDetailsInfoUpdateDto userDetailsInfoUpdateDto) {
        //检查传入的id是否正确
        UserDetailsInfo userDetailsInfo = userDetailsInfoDao.selectById(userDetailsInfoUpdateDto.getId());
        if(null == userDetailsInfo){
            log.info("修改的数据不存在");
            return null;
        }
        userDetailsInfo = ConvertBeanUtils.copyProperties(userDetailsInfoUpdateDto,UserDetailsInfo.class);
        return userDetailsInfoDao.updateById(userDetailsInfo);
    }

    /**
     * 根据ids查询用户的详细信息
     * @param ids
     * @return
     */
    @Override
    public List<UserDetailsInfoDto> findUserDetailsInfo(String ids) {
        List<Long> listIds = ListUtils.strToLongList(ids);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in(UserDetailsInfo.Meta.id,listIds);
        queryWrapper.eq(UserDetailsInfo.Meta.delFlag,0);
        List<UserDetailsInfo> userDetailsInfos = userDetailsInfoDao.selectList(queryWrapper);
        return ListUtils.copyList(userDetailsInfos,UserDetailsInfoDto.class);
    }

    /**
     * 根据id查询用户的详细信息
     * @param primaryId
     * @return
     */
    @Override
    public UserDetailsInfoDto findUserDetailsInfoById(Long primaryId){
        UserDetailsInfo userDetailsInfo = userDetailsInfoDao.selectById(primaryId);
        if(userDetailsInfo == null || userDetailsInfo.getDelFlag() != 0 ){
            return null;
        }
        return ConvertBeanUtils.copyProperties(userDetailsInfo,UserDetailsInfoDto.class);
    }

    @Override
    public UserDetailsInfoDto findUserDetailsInfoByUserId(Long userId) {
        MyAssert.notNull(userId,"查询用户的id不能为空");
        QueryWrapper<UserDetailsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(UserDetailsInfo.Meta.userAccountInfoId,userId).eq(UserDetailsInfo.Meta.delFlag,0);
        List<UserDetailsInfo> detailsInfos = userDetailsInfoDao.selectList(queryWrapper);
        return ListUtils.fetchOne(detailsInfos,UserDetailsInfoDto.class);
    }


}
